<!-- 收银台 -->
<template>
	<view class="pay-method-wrap">
		<view class="u-flex-col u-col-center money-box">
			<view class="money">{{ total_fee }}</view>
		</view>

		<!-- 支付方式单选项 -->
		<u-radio-group v-if="paymentList && paymentList.length" class="pay-box" v-model="payType"
			active-color="#FF9A33">
			<!-- 微信支付 -->
			<view class="u-flex u-row-between pay-item" v-show="paymentList.includes('wechat')"
				@tap="payType = 'wechat'">
				<view class="u-flex">
					<image class="pay-img" src="../../../static/order_wx_pay.png" mode=""></image>
					<text>微信支付</text>
				</view>
				<u-radio shape="circle" name="wechat"></u-radio>
			</view>
			<!-- 余额支付 -->
			<view class="u-flex u-row-between pay-item" v-show="paymentList.includes('wallet')"
				@tap="payType = 'wallet'">
				<view class="u-flex">
					<image class="pay-img" src="../../../static/order_wallet_pay.png" mode=""></image>
					<text>余额支付</text>
					<text style="margin-left: 30rpx;">￥{{money}}</text>
				</view>
				<u-radio shape="circle" name="wallet"></u-radio>
			</view>
			<view class="u-flex u-row-between pay-item" v-show="paymentList.includes('wallet')"
				@tap="payType = 'deduction'">
				<view class="u-flex">
					<image class="pay-img" src="../../../static/order_wallet_pay.png" mode=""></image>
					<text>抵扣卡支付</text>
				</view>
				<u-radio shape="circle" name="deduction"></u-radio>
			</view>
		</u-radio-group>

		<button class="u-reset-button pay-btn" @tap="confirmPay">去支付</button>
		<u-popup v-model="show" mode="bottom">
			<view style="height: 181px; display: flex;align-items: center;justify-content: center;">
				<view style="text-align: center;">
					<text style="display: block;padding: 32px;"> 您未授权使用手机号，商家无法联系您，请尽快授权手机号！</text>
					<u-button size="medium" class="u-reset-button pay-btn" open-type="getPhoneNumber" type="success"
						@getphonenumber="getphone">授权</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	let timer;
	export default {
		components: {},
		data() {
			return {
				payType: '', //支付方式
				appPlatfrom: '',
				orderType: '',
				IMG_URL: 'https://www.sdxiecheng.cn/',
				paymentList: [
					'wechat',
					'wallet',
					'deduction'
				],
				total_fee: '',
				order_id: '',
				detail_id: 0,
				money: 0, //余额
				show: false
			};
		},
		computed: {

		},
		onShow() {
			this.getmoney()
		},
		onLoad(e) {
			
			this.total_fee = e.amount
			this.order_id = e.order_id
			this.detail_id = e.detail_id
			this.getmoney()
		},
		methods: {
			getmoney() {
				uni.request({
					url:"https://www.sdxiecheng.cn/api/user/user_money",
					// url: 'https://www.sdxiecheng.cn/api/user/user_money',
					method: 'POST',
					data: {
						openid: uni.getStorageSync('openid'),
					},
					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {
							this.money = res.data.data.money
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							})
						}
					},
					fail: (err) => {
						console.log(err)
					}
				});
			},
			// 发起支付
			confirmPay() {
				let that = this;
				if (!uni.getStorageSync('phoneNumber')) {
					this.show = true;
				} else {
				let pay = null;
				if (!that.payType) {
					this.$u.toast('请选择支付方式');
					return;
				}
				if (that.payType == 'wallet') {
					uni.request({
						url: 'https://www.sdxiecheng.cn/addons/epay/api/pay_orderon',
						// url: 'https://www.sdxiecheng.cn/addons/epay/api/pay_orderon',
						method: 'POST',
						data: {
							user_id: uni.getStorageSync('userId'),
							openid: uni.getStorageSync('openid'),
							pay_type: 'balance',
							order_id: that.order_id
						},
						success: (res) => {
							console.log(res.data);
							if (res.data.code == 200) {
								if (res.data.data != '') {

								} else {
									uni.redirectTo({
										url: '../../pay/on-line_pay?order_id=' + that.order_id +
											'&detail_id=' + that.detail_id
									})
								}
							} else {
								uni.showToast({
									icon: 'none',
									title: res.data.msg
								})
							}

							// that.cabinet = res.data.data.cabinet
							// that.coupon = res.data.data.coupon
							// that.goods = res.data.data.goods
							// that.generateOrder()
						},
						fail: (err) => {
							console.log(err)
						}
					});
				} else if (that.payType == 'wechat') {
					uni.request({
						url: 'https://www.sdxiecheng.cn/addons/epay/api/pay_orderon',
						
						// url: 'https://www.sdxiecheng.cn/addons/epay/api/pay_orderon',
						method: 'POST',
						data: {
							user_id: uni.getStorageSync('userId'),
							openid: uni.getStorageSync('openid'),
							pay_type: 'wechat',
							order_id: that.order_id
						},
						success: (res) => {
							console.log(res.data);
							if (res.data.code == 200) {
								if (res.data.data != '') {
									wx.requestPayment({
										timeStamp: res.data.data.timeStamp + '',
										nonceStr: res.data.data.nonceStr,
										package: res.data.data.package,
										signType: res.data.data.signType,
										paySign: res.data.data.paySign,
										success: function(res) {
											console.log(res)
											uni.redirectTo({
												url: '../../pay/on-line_pay?order_id=' + that
													.order_id + '&detail_id=' + that
													.detail_id
											})
											// uni.redirectTo({
											//     url: '../myOrder/myOrder?index=2&type=shangpin'
											// });
										},
										fail: function(res) {

											// console.log(res.errMsg)
											uni.showToast({
												icon: 'none',
												title: '取消支付'
											})
											// uni.redirectTo({
											//     url: '../myOrder/myOrder?index=1&type=shangpin'
											// });
										},
										complete: function(res) {}
									})
								} else {
									uni.redirectTo({
										url: '../../pay/on-line_pay?order_id=' + that.order_id +
											'&detail_id=' + that.detail_id
									})
								}
							} else {
								uni.showToast({
									icon: 'none',
									title: res.data.msg
								})
							}

							// that.cabinet = res.data.data.cabinet
							// that.coupon = res.data.data.coupon
							// that.goods = res.data.data.goods
							// that.generateOrder()
						},
						fail: (err) => {
							console.log(err)
						}
					});
				} else if (that.payType == 'deduction') {
					uni.request({
						// url: 'https://www.sdxiecheng.cn/addons/epay/api/pay_orderon',
						url: 'https://www.sdxiecheng.cn/addons/epay/api/pay_orderon',

						method: 'POST',
						data: {
							user_id: uni.getStorageSync('userId'),
							openid: uni.getStorageSync('openid'),
							pay_type: 'card',
							order_id: that.order_id
						},
						success: (res) => {
							console.log(res.data);
							if (res.data.code == 200) {
								if (res.data.data != '') {

								} else {
									uni.redirectTo({
										url: '../../pay/on-line_pay?order_id=' + that.order_id +
											'&detail_id=' + that.detail_id
									})
								}
							} else {
								uni.showToast({
									icon: 'none',
									title: res.data.msg
								})
							}

							// that.cabinet = res.data.data.cabinet
							// that.coupon = res.data.data.coupon
							// that.goods = res.data.data.goods
							// that.generateOrder()
						},
						fail: (err) => {
							console.log(err)
						}
					});
				}}
			},
			
			getphone(e) {
				console.log(e, '=-=-=-=-=-=-65646546')
				if (!e.detail.iv) {
					uni.showToast({
						title: '获取手机号失败',
						icon: 'none'
					})
					return;
				}
				var that = this;
				uni.login({
					provider: 'weixin',
					success(res) {
						let code = res.code;
						var obj = {
							code,
							iv: e.detail.iv,
							encryptedData: e.detail.encryptedData
						}
						that.decryptPhone(obj);
					}
				})
			},
			decryptPhone(obj) {
				uni.$u.http.post('user/getMobile', obj).then(res => {
					this.postPhone(res.data.phoneNumber)
					// this.postPhone()
				})
			},
			// 提交手机号
			postPhone(phone) {

				uni.$u.http.post('user/binding', {
					mobile: phone,
					// openid: uni.getStorageSync('openid')
				}).then(res => {
					console.log(res, '授权  ')

					uni.setStorageSync('phoneNumber', phone)
					uni.showToast({
						icon: 'success',
						title: '授权成功'
					})
					this.show = false

				}).catch(err => {
					uni.showToast({
						icon: 'none',
						title: err.msg
					})
				})
			}
		}
	};
</script>

<style lang="scss">
	.pay-method-wrap {
		margin: 10rpx 20rpx;
		height: 100vh;
		background: #fff;
		border-radius: 20rpx;
		overflow: hidden;
	}

	.money-box {
		background: #fff;
		height: 250rpx;
		margin-bottom: 50rpx;
		padding-top: 30rpx;

		.time {
			font-size: 24rpx;
			color: #ACA9AB;
			padding: 8rpx 23rpx;
			background: #FFF9F3;
			margin-top: 49rpx;
		}

		.money {
			color: #FF9A33;
			font-size: 60rpx;
			margin-top: 60rpx;
			font-weight: bold;

			&::before {
				content: '￥';
				font-size: 36rpx;
			}
		}
	}

	.pay-box {
		.pay-item {
			height: 90rpx;
			padding: 0 30rpx;
			font-size: 26rpx;
			background: #fff;
			width: 750rpx;
			border-bottom: 1rpx solid #eeeeee;

			&:last-child {
				border-bottom: none;
			}

			.pay-img {
				width: 40rpx;
				height: 40rpx;
				margin-right: 25rpx;
			}
		}
	}

	.pay-btn {
		width: 500rpx;
		line-height: 80rpx;
		background: #FF9A33;
		border-radius: 40rpx;
		color: #fff;
		margin: 400rpx auto 0;
	}
</style>
